import { TiledTile, Vec2 } from "cc";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { smc } from "../../common/ecs/model/SingletonModuleComp";
import { ObstacleModelComp } from "./model/ObstacleModelComp";

/** 障碍物 */
@ecs.register("Obstacle")
export class Obstacle extends ecs.Entity {
  // 数据层
  ObstacleModel!: ObstacleModelComp;

  init() {
    this.add(ObstacleModelComp);
  }

  /** 加载障碍物 */
  load(
    tiled: TiledTile,
    tiledBox: { width: number; height: number },
    pos: Vec2
  ) {
    this.ObstacleModel.tiled = tiled;
    this.ObstacleModel.tiledBox = tiledBox;
    this.ObstacleModel.pos = pos;

    const simulator = smc.level.rov;

    const minX = pos.x - tiledBox.width / 2;
    const maxX = pos.x + tiledBox.width / 2;
    const minY = pos.y - tiledBox.height / 2;
    const maxY = pos.y + tiledBox.height / 2;
    simulator.addObstacle({ minX, maxX, minY, maxY });
  }
}
